//
//  YXBaseTableViewController.swift
//  微博YX
//
//  Created by apple on 15/11/23.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit



class YXBaseTableViewController: UITableViewController {
   var userLogin =  YXUserAccount.userLogin
    
    
    override  func loadView() {
        print("\(userLogin)")
        
        userLogin ? super.loadView() : setupVistorView()
        
    }
//    override func viewDidLoad() {
//        super.viewDidLoad()
//        view.frame = UIScreen.mainScreen().bounds
////        userLogin ? super.loadView() : setupVistorView()
//    }
    
    
    
    /// 创建访客视图
    func setupVistorView(){
     let vistorView =  YXVistorView()
     view = vistorView
     vistorView.vistorViewDelegate = self
        
        
//    设置导航栏
//        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewRegistClick")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewLoginClick")
        
        if self is YXHomeViewController {
//            开始动画
            vistorView.rotationAnimation()
//            监听应用退到后台
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
//            应用进入前台
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
        }else  if self is YXMessageViewController{
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
            
        }else  if self is YXProfileViewController{
             vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
            
        }else  if self is YXDiscoverViewController{
             vistorView.setupVistorView("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
            
        }
    }
//    暂停动画
    func didEnterBackground(){
//        (view as! YXVistorView).pauseAnimation()
    }
    /// MARK: -通知方法
//    开始动画
    func didBecomeActive(){
//        (view as! YXVistorView).resumeAnimation()
    }
    
    
    /// 移除通知
    deinit{
      if self is YXHomeViewController {
        NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
}
// MARK: - 扩展 CZBaseTableViewController 实现 CZVistorViewDelegte 协议
//相当于 category, 方便代码的管理
extension YXBaseTableViewController:YXVistorViewDelegate{
    ///MARK: -代理方法
    func vistorViewRegistClick(){
        print("注册按钮")
    }
    
    func vistorViewLoginClick(){
        let controller = YXOauthViewController()
        presentViewController(UINavigationController(rootViewController: controller), animated: true, completion: nil)
        
    }
}
